// 订单列表
var app =getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    all:[
      {
        value:"全部",
        checked:true
      },
      {
        value: "待付款",
        checked: false
      },
      {
        value: "待发货",
        checked: false
      },
      {
        value: "待收货",
        checked: false
      },
      {
        value: "待评价",
        checked: false
      },
    ],
    // 订单列表
    order:[],
    // 当前显示的列表的index
    status:0,
    noMore:false,
    p:1,
    scrollTop:"",
    // pay=true,正在支付，支付按钮不能点击
    pay:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.getStorage({
      key: 'order',
      success: function(res) {
        var status = res.data;
        // 改变列表的名称导航
        var all = this.data.all;
        
        // 根据点击显示列表
        if(status == undefined){
          this.getOrderAll()
        }else{
          this.getOrder(status)
          all.map(function (val) {
            val['checked'] = false;
            return val;
          })
          all[status].checked = true;
          this.setData({
            all: all,
            status:status,
            p:1,
            noMore:false
          })
        }
      }.bind(this),
      fail:function(res){
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel:false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.getStorage({
      key: 'order',
      success: function (res) {
        var status = res.data;
        // 改变列表的名称导航
        var all = this.data.all;

        // 根据点击显示列表
        if (status == undefined) {
          this.getOrderAll()
        } else {
          this.getOrder(status)
          all.map(function (val) {
            val['checked'] = false;
            return val;
          })
          all[status].checked = true;
          this.setData({
            all: all,
            status: status
          })
        }
        wx.stopPullDownRefresh()
      }.bind(this),
      fail: function (res) {
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel: false
        })
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.name,
      path: "/pages/shouye/shouye",
      imageUrl: app.globalData.headPic
    }
  },
  /**
   * 改变选中的状态
   */
  changeChecked:function(e){
    var index = e.currentTarget.dataset.index;
    var all = this.data.all;
    var status = "";
    all.map(function(val){
      val['checked'] = false;
      return val;
    })
    all[index].checked = true;
    
    // 根据选中状态改变列表
    if(index == 0){
      wx.setStorage({
        key: 'order',
        data: '0',
      })
      this.getOrderAll();
    }else if(index == 1){
      status = 1;
      wx.setStorage({
        key: 'order',
        data: '1',
      })
      this.getOrder(status);
    } else if (index == 2) {
      status = 2;
      wx.setStorage({
        key: 'order',
        data: '2',
      })
      this.getOrder(status);
    } else if (index == 3) {
      status = 3;
      wx.setStorage({
        key: 'order',
        data: '3',
      })
      this.getOrder(status);
    }else{
      status = 4;
      wx.setStorage({
        key: 'order',
        data: '4',
      })
      this.getOrder(status);
    }
    this.setData({
      status:status,
      all: all,
      noMore:false,
      p:1,
      scrollTop:0
    })
  },
  // 请求列表
  getOrder:function(status){
    var m_id = app.globalData.user.m_id;
    var p = 1;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/orderList',
      data:{
        m_id:m_id,
        p:p,
        status:status
      },
      header:{
        "content-type":"application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(res){
        var order = res.data.data;
        this.setData({
          order:order
        })
      }.bind(this),
      fail:function(res){
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel:false
        })
      }
    })
  },
  // 请求列表，全部列表
  getOrderAll: function () {
    var m_id = app.globalData.user.m_id;
    var p = 1;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/orderList',
      data: {
        m_id: m_id,
        p: p
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      success: function (res) {
        var order = res.data.data;
        this.setData({
          order: order
        })
      }.bind(this),
      fail: function (res) {
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel: false
        })
      }
    })
  },
  /**
   *  取消订单/删除订单
   */ 
  cancelOrder:function(e){
    var order_id = e.currentTarget.dataset.idx;
    var status = this.data.status;
    wx.showModal({
      title: '提示',
      content: '确定删除此订单？',
      success:function(res){
        // 确定删除订单
        if(res.confirm){
          wx.request({
            url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/delOrder',
            data: {
              order_id: order_id
            },
            header: {
              "content-type":"application/x-www-form-urlencoded"
            },
            method: "POST",
            success: function (res) {
              // 删除成功
              // wx.showModal({
              //   title: '提示',
              //   content: res.data.message,
              //   showCancel: false
              // })
              if (status === 0){
                this.getOrderAll()
              }else{
                this.getOrder(status)
              }
            }.bind(this),
            fail: function (res) {
              // 删除失败
              wx.showModal({
                title: '提示',
                content: res.data.message,
                showCancel: false
              })
            }.bind(this)
          })
        }
      }.bind(this),
      fail:function(res){
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel:false
        })
      }
    })
  },
  /**
   * 评价
   */
  assess:function(e){
      var order_id = e.currentTarget.dataset.oid;
      wx.setStorage({
        key: 'order_id',
        data: order_id,
        success:function(){
          wx.navigateTo({
            url: '/order/assess/assess',
          })
        }
      })
  },
  /**
   * 确认收货
   */
  sureReceived:function(e){
    var all = this.data.all;
    var order_id = e.currentTarget.dataset.oid;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/makeSure',
      data:{
        order_id: order_id
      },
      header:{
        "content-type":"application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(res){
        this.getOrder(4)
        all.map(function(val){
            val.checked = false
        })
        all[4].checked = true
        this.setData({
          status:4,
          all:all
        })
        
      }.bind(this)
    })
  },
  /**
   * 查看物流
   */
  showLine:function(e){
    var order_id = e.currentTarget.dataset.oid;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/lookSendMessage',
      data:{
        p_o_id:order_id
      },
      header:{
        "content-type":"application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(res){
       wx.showModal({
         title: '物流',
         content: res.data.data,
         showCancel:false
       })
      }
    })
  },
  /**
   * 支付
   */
  payment:function(e){
    this.setData({
      pay:true
    })
    var index = e.currentTarget.dataset.idx;
    var order = this.data.order[index];
    var shop_id = order.shop_id
    var m_id= app.globalData.user.m_id;
    var order_sn = order.order_sn;
    var all = this.data.all;
    wx.request({
      url: app.globalData.yuming +'merchant.php/Api/Pay/wxPay',
      data:{
        shop_id: shop_id,
        m_id: m_id,
        order_sn: order_sn
      },
      header:{
        "content-type":"application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(res){
        if (res.data.data == ""){
          wx.showModal({
            title: '提示',
            content: res.data.message,
            showCancel:false
          })
          this.setData({
            pay: false
          })
        }
        var data = JSON.parse(res.data).pay_message
        var message = JSON.parse(data);
        var timeStamp = message['timeStamp'];
        var nonceStr = message['nonceStr'];
        var packaged = message['package'];
        var signType = message['signType'];
        var paySign = message['paySign'];
        wx.requestPayment({
          timeStamp: timeStamp,
          nonceStr: nonceStr,
          package: packaged,
          signType: signType,
          paySign: paySign,
          success: function (res) {
            //  2 待发货
            this.getOrder(2)
            all.map(function (val) {
              val.checked = false
            })
            all[2].checked = true
            this.setData({
              status: 2,
              all: all
            })
          }.bind(this),
          fail: function (res) {
            this.setData({
              pay: false
            })
            //  1 待付款
            if (res.errMsg == "requestPayment:fail cancel") {
              this.getOrder(1)
              all.map(function (val) {
                val.checked = false
              })
              all[1].checked = true
              this.setData({
                status: 1,
                all: all
              })
            } else {
              wx.showModal({
                title: '提示',
                content: res.errMsg,
                showCancel: false
              })
            }
          }.bind(this)
        })
      }.bind(this),
      fail:function(res){
        this.setData({
          pay: false
        })
        wx.showModal({
          title: '提示',
          content: res.data.message,
        })
      }.bind(this)
    })
  },
  /**
   * 下滑分页
   */
  most:function(){
    var noMore = this.data.noMore;
    var p = this.data.p;
    p = p + 1;
    var status = this.data.status;
    var order = this.data.order;
    if(noMore == false){
      var m_id = app.globalData.user.m_id;
      wx.request({
        url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/orderList',
        data: {
          m_id: m_id,
          p: p,
          status: status
        },
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        success: function (res) {
          var ord = res.data.data;
          if(ord !=""){
           ord.map(function(val){
             order.push(val)
           })
          }else{
            noMore = true;
          }
          this.setData({
            p:p,
            order: order,
            noMore: noMore
          })
        }.bind(this),
        fail: function (res) {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            showCancel: false
          })
        }
      })
    }
  },
  detail:function(e){
    var order_id = e.currentTarget.dataset.oid;
    console.log(order_id)
    wx.setStorage({
      key: 'order_id',
      data: order_id,
      success:function(){
        wx.navigateTo({
          url: '/mine/order/order',
        })
      }
    })
  }
})